package fun.zhufn.oldcomm.view;

import fun.zhufn.oldcomm.OldManagerController;
import fun.zhufn.oldcomm.model.bo.Old;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Objects;
import java.util.logging.Logger;

public class ModifyOld {
    private static final OldManagerController oldContro = OldManagerController.getInstance();
    private static final Logger log = Logger.getLogger(ModifyOld.class.toString());
    private JFrame frame, father;
    private Old ori = null;
    private boolean isNew = false;

    public ModifyOld() {
        $$$setupUI$$$();
        cancel_jb.addActionListener(e -> {
            frame.dispose();
            father.setVisible(true);
        });
        ok_jb.addActionListener(e -> {
            Old u = new Old(name_jtf.getText(), Objects.equals(sex_jcb.getSelectedItem(), "男"), birthday_jftf.getText(), phone_jftf.getText(), ori == null ? null : ori.getServer(), ori == null ? 0 : ori.getId());
            try {
                Long.parseLong(birthday_jftf.getText());
                Long.parseLong(phone_jftf.getText());
            } catch (Exception err) {
                JOptionPane.showMessageDialog(null, "生日和电话号码仅支持输入数字");
                return;
            }

            if (u.getBirthday().length() != 8 || u.getPhone().length() != 11) {
                JOptionPane.showMessageDialog(null, "请输入8位生日及11位电话号码");
                return;
            }

            if (u.getName().equals("")) {
                JOptionPane.showMessageDialog(null, "请不要留空");
                return;
            }
            if (!isNew) oldContro.editOld(u);
            else oldContro.addOld(u);

            frame.dispose();
            father.setVisible(true);
        });
    }

    /**
     * @param old 启动界面
     * @param fa 父界面
     */
    public void start(Old old, JFrame fa) {
        fa.setVisible(false);
        this.isNew = old == null;
        ori = old;
        father = fa;
        frame = new JFrame("ModifyOld");
        var ins = this;
        frame.setContentPane(ins.mu_jp);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setMinimumSize(frame.getSize());
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                father.setVisible(true);
            }
        });
        if (old != null) {
            name_jtf.setText(old.getName());
            birthday_jftf.setText(old.getBirthday());
            phone_jftf.setText(old.getPhone());
            sex_jcb.setSelectedItem(old.isMale() ? "男" : "女");
        }
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        int x = (int) (toolkit.getScreenSize().getWidth() - frame.getWidth()) / 2;
        int y = (int) (toolkit.getScreenSize().getHeight() - frame.getHeight()) / 2;
        frame.setLocation(x, y);
    }

    private JPanel mu_jp;
    private JLabel name_jl;
    private JLabel birthday_jl;
    private JLabel phone_jl;
    private JLabel sex_jl;
    private JTextField account_jtf;
    private JTextField name_jtf;
    private JFormattedTextField birthday_jftf;
    private JFormattedTextField phone_jftf;
    private JComboBox<String> sex_jcb;
    private JComboBox<String> role_jcb;
    private JButton ok_jb;
    private JButton cancel_jb;


    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        mu_jp = new JPanel();
        mu_jp.setLayout(new GridBagLayout());
        mu_jp.setBackground(new Color(-1116681));
        name_jl = new JLabel();
        name_jl.setText("姓名");
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 0);
        mu_jp.add(name_jl, gbc);
        birthday_jl = new JLabel();
        birthday_jl.setText("生日");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 0);
        mu_jp.add(birthday_jl, gbc);
        phone_jl = new JLabel();
        phone_jl.setText("电话号码");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 0);
        mu_jp.add(phone_jl, gbc);
        sex_jl = new JLabel();
        sex_jl.setText("性别");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 0);
        mu_jp.add(sex_jl, gbc);
        name_jtf = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.weightx = 5.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(name_jtf, gbc);
        birthday_jftf = new JFormattedTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        gbc.weightx = 5.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(birthday_jftf, gbc);
        phone_jftf = new JFormattedTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        gbc.weightx = 5.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(phone_jftf, gbc);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        gbc.weightx = 5.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(sex_jcb, gbc);
        ok_jb = new JButton();
        ok_jb.setBackground(new Color(-5310023));
        ok_jb.setText("确认");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.weightx = 2.5;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.SOUTH;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mu_jp.add(ok_jb, gbc);
        cancel_jb = new JButton();
        cancel_jb.setText("取消");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 4;
        gbc.weightx = 2.5;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.SOUTH;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mu_jp.add(cancel_jb, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return mu_jp;
    }

    private void createUIComponents() {
        account_jtf = new JTextField();
        account_jtf.setPreferredSize(new Dimension(200, 20));

        sex_jcb = new JComboBox<>(new String[]{"男", "女"});
        role_jcb = new JComboBox<>(new String[]{"管理员", "生活管家", "后勤管理"});
    }
}